home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / FILE.C < prev    next >
C/C++ Source or Header  |  1994-04-05  |  13KB  |  515 lines

  1. /* Target file hash table management for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24.  
  25.  
  26. /* Hash table of files the makefile knows how to make.  */
  27.  
  28. #ifndef    FILE_BUCKETS
  29. #define FILE_BUCKETS    1007
  30. #endif
  31. static struct file *files[FILE_BUCKETS];
  32.  
  33. /* Number of files with the `intermediate' flag set.  */
  34.  
  35. unsigned int num_intermediates = 0;
  36.  
  37.  
  38. /* Access the hash table of all file records.
  39.    lookup_file  given a name, return the struct file * for that name,
  40.            or nil if there is none.
  41.    enter_file   similar, but create one if there is none.  */
  42.  
  43. struct file *
  44. lookup_file (name)
  45.      char *name;
  46. {
  47.   register struct file *f;
  48.   register char *n;
  49.   register unsigned int hashval;
  50.  
  51.   if (*name == '\0')
  52.     abort ();
  53.  
  54.   /* This is also done in parse_file_seq, so this is redundant
  55.      for names read from makefiles.  It is here for names passed
  56.      on the command line.  */
  57.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  58.     {
  59.       name += 2;
  60.       while (*name == '/')
  61.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  62.     ++name;
  63.     }
  64.  
  65.   if (*name == '\0')
  66.     /* It was all slashes after a dot.  */
  67.     name = "./";
  68.  
  69.   hashval = 0;
  70.   for (n = name; *n != '\0'; ++n)
  71.     HASH (hashval, *n);
  72.   hashval %= FILE_BUCKETS;
  73.  
  74.   for (f = files[hashval]; f != 0; f = f->next)
  75.     if (streq (f->name, name))
  76.       return f;
  77.   return 0;
  78. }
  79.  
  80. struct file *
  81. enter_file (name)
  82.      char *name;
  83. {
  84.   register struct file *f, *new;
  85.   register char *n;
  86.   register unsigned int hashval;
  87.  
  88.   if (*name == '\0')
  89.     abort ();
  90.  
  91.   hashval = 0;
  92.   for (n = name; *n != '\0'; ++n)
  93.     HASH (hashval, *n);
  94.   hashval %= FILE_BUCKETS;
  95.  
  96.   for (f = files[hashval]; f != 0; f = f->next)
  97.     if (streq (f->name, name))
  98.       break;
  99.  
  100.   if (f != 0 && !f->double_colon)
  101.     return f;
  102.  
  103.   new = (struct file *) xmalloc (sizeof (struct file));
  104.   bzero ((char *) new, sizeof (struct file));
  105.   new->name = name;
  106.   new->update_status = -1;
  107.  
  108.   if (f == 0)
  109.     {
  110.       /* This is a completely new file.  */
  111.       new->next = files[hashval];
  112.       files[hashval] = new;
  113.     }
  114.   else
  115.     {
  116.       /* There is already a double-colon entry for this file.  */
  117.       new->double_colon = f;
  118.       while (f->prev != 0)
  119.     f = f->prev;
  120.       f->prev = new;
  121.     }
  122.  
  123.   return new;
  124. }
  125.  
  126. /* Rename FILE to NAME.  This is not as simple as resetting
  127.    the `name' member, since it must be put in a new hash bucket,
  128.    and possibly merged with an existing file called NAME.  */
  129.  
  130. void
  131. rename_file (file, name)
  132.      register struct file *file;
  133.      char *name;
  134. {
  135.   char *oldname = file->name;
  136.   register unsigned int oldhash;
  137.   register char *n;
  138.  
  139.   while (file->renamed != 0)
  140.     file = file->renamed;
  141.  
  142.   /* Find the hash values of the old and new names.  */
  143.  
  144.   oldhash = 0;
  145.   for (n = oldname; *n != '\0'; ++n)
  146.     HASH (oldhash, *n);
  147.  
  148.   file_hash_enter (file, name, oldhash, file->name);
  149. }
  150.  
  151. void
  152. file_hash_enter (file, name, oldhash, oldname)
  153.      register struct file *file;
  154.      char *name;
  155.      unsigned int oldhash;
  156.      char *oldname;
  157. {
  158.   unsigned int oldbucket = oldhash % FILE_BUCKETS;
  159.   register unsigned int newhash, newbucket;
  160.   struct file *oldfile;
  161.   register char *n;
  162.   register struct file *f;
  163.  
  164.   newhash = 0;
  165.   for (n = name; *n != '\0'; ++n)
  166.     HASH (newhash, *n);
  167.   newbucket = newhash % FILE_BUCKETS;
  168.  
  169.   /* Look for an existing file under the new name.  */
  170.  
  171.   for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
  172.     if (streq (oldfile->name, name))
  173.       break;
  174.  
  175.   if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
  176.     {
  177.       /* Remove FILE from its hash bucket.  */
  178.  
  179.       struct file *lastf = 0;
  180.  
  181.       for (f = files[oldbucket]; f != file; f = f->next)
  182.     lastf = f;
  183.  
  184.       if (lastf == 0)
  185.     files[oldbucket] = f->next;
  186.       else
  187.     lastf->next = f->next;
  188.     }
  189.  
  190.   /* Give FILE its new name.  */
  191.  
  192.   file->name = name;
  193.   for (f = file->double_colon; f != 0; f = f->prev)
  194.     f->name = name;
  195.  
  196.   if (oldfile == 0)
  197.     {
  198.       /* There is no existing file with the new name.  */
  199.  
  200.       if (newbucket != oldbucket)
  201.     {
  202.       /* Put FILE in its new hash bucket.  */
  203.       file->next = files[newbucket];
  204.       files[newbucket] = file;
  205.     }
  206.     }
  207.   else
  208.     {
  209.       /* There is an existing file with the new name.
  210.      We must merge FILE into the existing file.  */
  211.  
  212.       register struct dep *d;
  213.  
  214.       if (file->cmds != 0)
  215.     {
  216.       if (oldfile->cmds == 0)
  217.         oldfile->cmds = file->cmds;
  218.       else if (file->cmds != oldfile->cmds)
  219.         {
  220.           /* We have two sets of commands.  We will go with the
  221.          one given in the rule explicitly mentioning this name,
  222.          but give a message to let the user know what's going on.  */
  223.           if (oldfile->cmds->filename != 0)
  224.         makefile_error (file->cmds->filename, file->cmds->lineno,
  225.                 "Commands were specified for \
  226. file `%s' at %s:%u,",
  227.                 oldname, oldfile->cmds->filename,
  228.                 oldfile->cmds->lineno);
  229.           else
  230.         makefile_error (file->cmds->filename, file->cmds->lineno,
  231.                 "Commands for file `%s' were found by \
  232. implicit rule search,",
  233.                 oldname);
  234.           makefile_error (file->cmds->filename, file->cmds->lineno,
  235.                   "but `%s' is now considered the same file \
  236. as `%s'.",
  237.                   oldname, name);
  238.           makefile_error (file->cmds->filename, file->cmds->lineno,
  239.                   "Commands for `%s' will be ignored \
  240. in favor of those for `%s'.",
  241.                   name, oldname);
  242.         }
  243.     }
  244.  
  245.       /* Merge the dependencies of the two files.  */
  246.  
  247.       d = oldfile->deps;
  248.       if (d == 0)
  249.     oldfile->deps = file->deps;
  250.       else
  251.     {
  252.       while (d->next != 0)
  253.         d = d->next;
  254.       d->next = file->deps;
  255.     }
  256.  
  257.       merge_variable_set_lists (&oldfile->variables, file->variables);
  258.  
  259.       if (oldfile->double_colon && !file->double_colon)
  260.     fatal ("can't rename single-colon `%s' to double-colon `%s'",
  261.            oldname, name);
  262.       if (!oldfile->double_colon && file->double_colon)
  263.     fatal ("can't rename double-colon `%s' to single-colon `%s'",
  264.            oldname, name);
  265.  
  266.       if (file->last_mtime > oldfile->last_mtime)
  267.     /* %%% Kludge so -W wins on a file that gets vpathized.  */
  268.     oldfile->last_mtime = file->last_mtime;
  269.  
  270. #define MERGE(field) oldfile->field |= file->field
  271.       MERGE (precious);
  272.       MERGE (tried_implicit);
  273.       MERGE (updating);
  274.       MERGE (updated);
  275.       MERGE (is_target);
  276.       MERGE (cmd_target);
  277.       MERGE (phony);
  278. #undef MERGE
  279.  
  280.       file->renamed = oldfile;
  281.     }
  282. }
  283.  
  284. /* Remove all nonprecious intermediate files.
  285.    If SIG is nonzero, this was caused by a fatal signal,
  286.    meaning that a different message will be printed, and
  287.    the message will go to stderr rather than stdout.  */
  288.  
  289. void
  290. remove_intermediates (sig)
  291.      int sig;
  292. {
  293.   register int i;
  294.   register struct file *f;
  295.   char doneany;
  296.   
  297.   if (!sig && just_print_flag)
  298.     return;
  299.  
  300.   doneany = 0;
  301.   for (i = 0; i < FILE_BUCKETS; ++i)
  302.     for (f = files[i]; f != 0; f = f->next)
  303.       if (f->intermediate && (f->dontcare || !f->precious))
  304.     {
  305.       int status;
  306.       if (just_print_flag)
  307.         status = 0;
  308.       else
  309.         {
  310.           status = unlink (f->name);
  311.           if (status < 0 && errno == ENOENT)
  312.         continue;
  313.         }
  314.       if (!f->dontcare)
  315.         {
  316.           if (sig)
  317.         error ("*** Deleting intermediate file `%s'", f->name);
  318.           else if (!silent_flag)
  319.         {
  320.           if (! doneany)
  321.             {
  322.               fputs ("rm ", stdout);
  323.               doneany = 1;
  324.             }
  325.           else
  326.             putchar (' ');
  327.           fputs (f->name, stdout);
  328.           fflush (stdout);
  329.         }
  330.           if (status < 0)
  331.         perror_with_name ("unlink: ", f->name);
  332.         }
  333.     }
  334.  
  335.   if (doneany && !sig)
  336.     {
  337.       putchar ('\n');
  338.       fflush (stdout);
  339.     }
  340. }
  341.  
  342. /* For each dependency of each file, make the `struct dep' point
  343.    at the appropriate `struct file' (which may have to be created).
  344.  
  345.    Also mark the files depended on by .PRECIOUS and .PHONY.  */
  346.  
  347. void
  348. snap_deps ()
  349. {
  350.   register struct file *f, *f2;
  351.   register struct dep *d;
  352.   register int i;
  353.  
  354.   /* Enter each dependency name as a file.  */
  355.   for (i = 0; i < FILE_BUCKETS; ++i)
  356.     for (f = files[i]; f != 0; f = f->next)
  357.       for (f2 = f; f2 != 0; f2 = f2->prev)
  358.     for (d = f2->deps; d != 0; d = d->next)
  359.       if (d->name != 0)
  360.         {
  361.           d->file = lookup_file (d->name);
  362.           if (d->file == 0)
  363.         d->file = enter_file (d->name);
  364.           else
  365.         free (d->name);
  366.           d->name = 0;
  367.         }
  368.   
  369.   for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
  370.     for (d = f->deps; d != 0; d = d->next)
  371.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  372.     f2->precious = 1;
  373.  
  374.   for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
  375.     for (d = f->deps; d != 0; d = d->next)
  376.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  377.     {
  378.       /* Mark this file as phony and nonexistent.  */
  379.       f2->phony = 1;
  380.       f2->last_mtime = (time_t) -1;
  381.     }
  382.  
  383.   f = lookup_file (".EXPORT_ALL_VARIABLES");
  384.   if (f != 0 && f->is_target)
  385.     export_all_variables = 1;
  386. }
  387.  
  388. /* Print the data base of files.  */
  389.  
  390. static void
  391. print_file (f)
  392.      struct file *f;
  393. {
  394.   register struct dep *d;
  395.  
  396.   putchar ('\n');
  397.   if (!f->is_target)
  398.     puts ("# Not a target:");
  399.   printf ("%s:%s", f->name, f->double_colon ? ":" : "");
  400.           
  401.   for (d = f->deps; d != 0; d = d->next)
  402.     printf (" %s", dep_name (d));
  403.   putchar ('\n');
  404.           
  405.   if (f->precious)
  406.     puts ("#  Precious file (dependency of .PRECIOUS).");
  407.   if (f->phony)
  408.     puts ("#  Phony target (dependency of .PHONY).");
  409.   if (f->cmd_target)
  410.     puts ("#  Command-line target.");
  411.   if (f->dontcare)
  412.     puts ("#  A default or MAKEFILES makefile.");
  413.   printf ("#  Implicit rule search has%s been done.\n",
  414.       f->tried_implicit ? "" : " not");
  415.   if (f->stem != 0)
  416.     printf ("#  Implicit/static pattern stem: `%s'\n", f->stem);
  417.   if (f->intermediate)
  418.     puts ("#  File is an intermediate dependency.");
  419.   if (f->also_make != 0)
  420.     {
  421.       fputs ("#  Also makes:", stdout);
  422.       for (d = f->also_make; d != 0; d = d->next)
  423.     printf (" %s", dep_name (d));
  424.       putchar ('\n');
  425.     }
  426.   if (f->last_mtime == (time_t) 0)
  427.     puts ("#  Modification time never checked.");
  428.   else if (f->last_mtime == (time_t) -1)
  429.     puts ("#  File does not exist.");
  430.   else
  431.     printf ("#  Last modified %.24s (%ld)\n",
  432.         ctime (&f->last_mtime), (long int) f->last_mtime);
  433.   printf ("#  File has%s been updated.\n",
  434.       f->updated ? "" : " not");
  435.   switch (f->command_state)
  436.     {
  437.     case cs_running:
  438.       puts ("#  Commands currently running (THIS IS A BUG).");
  439.       break;
  440.     case cs_deps_running:
  441.       puts ("#  Dependencies commands running (THIS IS A BUG).");
  442.       break;
  443.     case cs_not_started:
  444.     case cs_finished:
  445.       switch (f->update_status)
  446.     {
  447.     case -1:
  448.       break;
  449.     case 0:
  450.       puts ("#  Successfully updated.");
  451.       break;
  452.     case 1:
  453.       puts ("#  Failed to be updated.");
  454.       break;
  455.     default:
  456.       puts ("#  Invalid value in `update_status' member!");
  457.       fflush (stdout);
  458.       fflush (stderr);
  459.       abort ();
  460.     }
  461.       break;
  462.     default:
  463.       puts ("#  Invalid value in `command_state' member!");
  464.       fflush (stdout);
  465.       fflush (stderr);
  466.       abort ();
  467.     }
  468.  
  469.   if (f->variables != 0)
  470.     print_file_variables (f);
  471.  
  472.   if (f->cmds != 0)
  473.     print_commands (f->cmds);
  474. }
  475.  
  476. void
  477. print_file_data_base ()
  478. {
  479.   register unsigned int i, nfiles, per_bucket;
  480.   register struct file *file;
  481.  
  482.   puts ("\n# Files");
  483.  
  484.   per_bucket = nfiles = 0;
  485.   for (i = 0; i < FILE_BUCKETS; ++i)
  486.     {
  487.       register unsigned int this_bucket = 0;
  488.  
  489.       for (file = files[i]; file != 0; file = file->next)
  490.     {
  491.       register struct file *f;
  492.  
  493.       ++this_bucket;
  494.  
  495.       for (f = file; f != 0; f = f->prev)
  496.         print_file (f);
  497.     }
  498.  
  499.       nfiles += this_bucket;
  500.       if (this_bucket > per_bucket)
  501.     per_bucket = this_bucket;
  502.     }
  503.  
  504.   if (nfiles == 0)
  505.     puts ("\n# No files.");
  506.   else
  507.     {
  508.       printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
  509. #ifndef    NO_FLOAT
  510.       printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
  511.           ((double) nfiles) / ((double) FILE_BUCKETS) * 100.0, per_bucket);
  512. #endif
  513.     }
  514. }
  515.